package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hanlink.dsp.util.Constants;
import com.qztech.sdk.R;

/**
 * @author Zhanghb
 * Email: 2573475062@qq.com
 * Date : 2019-09-07 22:17
 */
public class BassCommonSeekBar extends LinearLayout {

    private TextView mTvTitle;
    private TextView mTvValue;
    private VerticalSeekBar mSeekBar;
    private ImageButton mBtnAdd;
    private ImageButton mBtnDesc;

    private String unit;
    private CharSequence[] mArray;
    private boolean isArrayClickChanged;//点击变化
    private int position;
    private BassPopWindow mArrayPopupWindow;
    private OnSeekBarChangeListener mOnSeekBarChangeListener;
    private long mLastChangedTs;

    public interface OnSeekBarChangeListener {
        void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser, boolean sendCmd);

        void onSlope(int index);
    }

    public BassCommonSeekBar(Context context) {
        super(context);
        setupView(null);
    }

    public BassCommonSeekBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setupView(attrs);
    }

    public BassCommonSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setupView(attrs);
    }

    private void setupView(AttributeSet attrs) {
        View.inflate(getContext(), com.hanlink.res.R.layout.bass_layout_common_seek_bar, this);
        mTvTitle = findViewById(com.hanlink.res.R.id.tvTitle);
        mTvValue = findViewById(com.hanlink.res.R.id.tvValue);
        mBtnAdd = findViewById(com.hanlink.res.R.id.btnAdd);
        mBtnDesc = findViewById(com.hanlink.res.R.id.btnDesc);
        mSeekBar = findViewById(com.hanlink.res.R.id.sbProgress);

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, com.hanlink.res.R.styleable.BassCommonSeekBar);
            unit = typedArray.getString(com.hanlink.res.R.styleable.BassCommonSeekBar_unit);
            int progress = typedArray.getInt(com.hanlink.res.R.styleable.BassCommonSeekBar_progress, 0);
            int max = typedArray.getInt(com.hanlink.res.R.styleable.BassCommonSeekBar_max, 100);
            mArray = typedArray.getTextArray(com.hanlink.res.R.styleable.BassCommonSeekBar_array);
            isArrayClickChanged = typedArray.getBoolean(com.hanlink.res.R.styleable.BassCommonSeekBar_array_click_changed, false);
            mSeekBar.setMax(max);
            mSeekBar.setProgress(progress);
            String title = typedArray.getString(com.hanlink.res.R.styleable.BassCommonSeekBar_title);
            mTvTitle.setText(title);
            int spinnerVisibility = typedArray.getInt(com.hanlink.res.R.styleable.BassCommonSeekBar_spinnerVisibility, View.GONE);
            typedArray.recycle();
        }

        mTvValue.setText(mSeekBar.getProgress() + unit);
        mBtnAdd.setOnClickListener(v -> mSeekBar.setProgress(mSeekBar.getProgress() + 1));
        mBtnDesc.setOnClickListener(v -> mSeekBar.setProgress(mSeekBar.getProgress() - 1));
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mOnSeekBarChangeListener != null) {
                    if (System.currentTimeMillis() - mLastChangedTs > Constants.SEEK_BAR_DELAY) {
                        mLastChangedTs = System.currentTimeMillis();
                        mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser, true);
                    } else {
                        mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser, false);
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mOnSeekBarChangeListener != null) {
                    mOnSeekBarChangeListener.onProgressChanged(seekBar, seekBar.getProgress(), true, true);
                }
            }
        });

        mTvValue.setOnClickListener(v -> {
            if (mArray == null) {
                return;
            }
            if (isArrayClickChanged) {
                changeArrayPosition((position + 1) % mArray.length);
                return;
            }
            showArrayPopWindow();
        });
    }

    public void initData(int progress, int slope, int subTitlePosition) {
        mSeekBar.setProgress(progress);
        position = slope;
        if (mArray != null) {
            mTvValue.setText(mArray[position]);
        }
    }

    public void setArray(int resId, int position) {
        mArray = getContext().getResources().getStringArray(resId);
        this.position = position % mArray.length;
        mTvValue.setText(mArray[position]);
    }

    public void setDisplayValue(String content) {
        mTvValue.setText(content + unit);
    }

    public int getProgress() {
        return mSeekBar.getProgress();
    }

    public int getPosition() {
        return position;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        mTvValue.setEnabled(enabled);
        mSeekBar.setEnabled(enabled);
        mTvValue.setEnabled(enabled);
        mBtnAdd.setEnabled(enabled);
        mBtnDesc.setEnabled(enabled);
        mTvTitle.setEnabled(enabled);
    }

    private void showArrayPopWindow() {
        mArrayPopupWindow = new BassPopWindow(getContext(), mArray, mTvValue.getMeasuredWidth(), position);
        mArrayPopupWindow.showAsDropDown(mTvValue);
        mArrayPopupWindow.setOnDismissListener(() -> changeArrayPosition(mArrayPopupWindow.getPosition()));
    }


    private void changeArrayPosition(int position) {
        this.position = position;
        mTvValue.setText(mArray[position]);
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onSlope(this.position);
        }
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener mOnSeekBarChangeListener) {
        this.mOnSeekBarChangeListener = mOnSeekBarChangeListener;
    }
}
